/**
 * 中国9号健康产业有限公司
 * Copyright (C) JiuHaoHealth Corporation. All Rights Reserved
 */
package com.jiuhaohealth.kangxun360.account.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.jiuhaohealth.kangxun360.account.AccountDao;
import com.jiuhaohealth.kangxun360.main.mapper.AccountMapper;
import com.jiuhaohealth.kangxun360.main.po.Account;
import com.jiuhaohealth.kangxun360.main.po.AccountCriteria;
import com.jiuhaohealth.kangxun360.main.po.AccountCriteria.Criteria;
import com.jiuhaohealth.kangxun360.main.po.AccountKey;
/**
 * @author minghui(jackchenmh@gmail.com)
 * @version 2014-3-18 下午5:08:45
 */
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {

	protected final static Logger logger = LoggerFactory.getLogger(AccountDaoImpl.class);
	
	@Resource(name = "accountMapper")
	private AccountMapper accountMapper;
	
	@Override
	public Integer deleteAccountById(AccountKey accountKey) {
		 AccountCriteria accountCriteria = new AccountCriteria();
			Criteria criteria = accountCriteria.createCriteria();
			criteria.andIdEqualTo(accountKey.getId());
			criteria.andUseridEqualTo(accountKey.getUserid());
			
		return accountMapper.deleteByCriteria(accountCriteria);
	}

	@Override
	public Integer insertAccount(Account account) {
		return accountMapper.insertSelective(account);
	}

	@Override
	public Account selectAccountByPk(AccountKey accountKey) {
		
		AccountCriteria accountCriteria = new AccountCriteria();
		Criteria criteria = accountCriteria.createCriteria();
		criteria.andIdEqualTo(accountKey.getId());
		criteria.andUseridEqualTo(accountKey.getUserid());
		List<Account>  accounts = accountMapper.selectByCriteria(accountCriteria);
		if(accounts.size()!=0){
			return accounts.get(0);
		}else{
			return null;
		}
	}

	@Override
	public List<Account> selectAccounts(String firstname , String lastname) {
		AccountCriteria accountCriteria = new AccountCriteria();
		Criteria criteria = accountCriteria.createCriteria();
		criteria.andFirstnameEqualTo(firstname);
		criteria.andLastnameEqualTo(lastname);
		return accountMapper.selectByCriteria(accountCriteria);
	}

	@Override
	public List<Account> selectAccounts(String firstname , Integer pageSize, Integer pageNo) {
		AccountCriteria accountCriteria = new AccountCriteria();
		accountCriteria.setOrderByClause("id desc");
		accountCriteria.setSkipCount(pageNo * pageSize - pageSize);
		accountCriteria.setTop(pageSize);
		Criteria criteria = accountCriteria.createCriteria();
		criteria.andFirstnameEqualTo(firstname);
		return accountMapper.selectByCriteria(accountCriteria);
	}

	@Override
	public Integer updateAccount(Account account) {
		return accountMapper.updateByPrimaryKeySelective(account);
	}

	@Override
	public Integer countByUserId(String userId) {
		AccountCriteria accountCriteria = new AccountCriteria();
		Criteria criteria = accountCriteria.createCriteria();
		criteria.andUseridEqualTo(userId);
		return accountMapper.countByCriteria(accountCriteria);
	}

}
